package cn.krui.helper.tools;

import java.math.BigDecimal;
import javax.servlet.http.HttpServletRequest;

/**
 * @author zc
 */
public class Param {
  static HttpServletRequest request() {
    return WebToolsUtils.getRequest();
  }
  
  public static Integer getInt(String name, Integer defValue, Integer min, Integer max) {
    String v = request().getParameter(name);
    if (v == null){
      return defValue; 
    }
    try {
      Integer val = Integer.valueOf(v);
      if (min != null && val.intValue() < min.intValue()){
        return min; 
      }
      if (max != null && val.intValue() > max.intValue()){
        return max; 
      }
      return val;
    } catch (NumberFormatException e) {
      return defValue;
    } 
  }
  
  public static Integer getInt(String name, Integer defValue) {
    return getInt(name, defValue, null, null);
  }
  
  public static int getInt(String name) {
    return getInt(name, null).intValue();
  }
  
  public static Long getLong(String name, Long defValue, Long min, Long max) {
    String v = request().getParameter(name);
    if (v == null){
      return defValue; 
    }
    try {
      Long val = Long.valueOf(v);
      if (min != null && val.longValue() < min.longValue()){
        return min; 
      }
      if (max != null && val.longValue() > max.longValue()){
        return max; 
      }
      return val;
    } catch (NumberFormatException e) {
      return defValue;
    } 
  }
  
  public static Long getLong(String name, Long defValue) {
    return getLong(name, defValue, null, null);
  }
  
  public static Long getLong(String name) {
    return getLong(name, null, null, null);
  }
  
  public static Double getDouble(String name, Double defValue, Double min, Double max) {
    String v = request().getParameter(name);
    if (v == null){
      return defValue; 
    }
    try {
      Double val = Double.valueOf(v);
      if (min != null && val.doubleValue() < min.doubleValue()){
        return min; 
      }
      if (max != null && val.doubleValue() > max.doubleValue()){
        return max; 
      }
      return val;
    } catch (NumberFormatException e) {
      return defValue;
    } 
  }
  
  public static BigDecimal getDecimal(String name, BigDecimal defValue, BigDecimal min, BigDecimal max) {
    String v = request().getParameter(name);
    if (v == null){
      return defValue; 
    }
    try {
      BigDecimal val = new BigDecimal(v);
      if (min != null && val.compareTo(min) == -1){
        return min; 
      }
      if (max != null && val.compareTo(max) == 1){
        return max; 
      }
      return val;
    } catch (NumberFormatException e) {
      return defValue;
    } 
  }
  
  public static BigDecimal getDecimal(String name, BigDecimal defValue) {
    return getDecimal(name, defValue, null, null);
  }
  
  public static BigDecimal getDecimal(String name) {
    return getDecimal(name, null, null, null);
  }
  
  public static Double getDouble(String name, Double defValue) {
    return getDouble(name, defValue, null, null);
  }
  
  public static Double getDouble(String name) {
    return getDouble(name);
  }
  
  public static String getString(String name, String defValue) {
    String v = request().getParameter(name);
    if (v == null){
      return defValue; 
    }
    return v;
  }
  
  public static String getString(String name) {
    return getString(name, null);
  }
}
